package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/channel/model"
)

type ChannelMapper struct {
}

/**
 * 查找栏目信息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *ChannelMapper) FindChannel(systemId uint64) ([]model.Channel, error) {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 准备一个切片容器
	var channels []model.Channel
	// 执行查询
	_, err := mysql.
		Raw("select id,title,icon,url,new_flag,hot_flag,sorted from xk_channel where  status = 1 and is_deleted = 0 and system_id = ? order by sorted ", systemId).
		QueryRows(&channels)
	if err != nil {
		return nil, err
	}
	return channels, nil
}

/**
 * 查找空间栏目信息
 * @author feige
 * @date 2023-11-03
 * @version 1.0
 * @desc
 */
func (mapper *ChannelMapper) FindChannelZone(systemId uint64) ([]model.Channel, error) {
	// 获取数据库链接
	mysql := orm.NewOrm()
	// 准备一个切片容器
	var channels []model.Channel
	// 执行查询
	_, err := mysql.
		Raw("select id,title,url,qname,icon,qurl,new_flag,hot_flag,sorted from xk_channel where  status = 1 and q_flag = 1 and is_deleted = 0 and system_id = ? order by sorted ", systemId).
		QueryRows(&channels)
	if err != nil {
		return nil, err
	}
	return channels, nil
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询栏目信息
 */
func (mapper *ChannelMapper) GetById(id uint64, systemId uint64) (channel *model.Channel, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbChannel := model.Channel{Id: id, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbChannel, "id", "system_id")
	// 4: 结构返回
	return &dbChannel, err
}

/**
 * 查询用户是否购买栏目
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *ChannelMapper) CountUserBuyChannelNo(userId uint64, channelId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_channel_user_buy WHERE user_id  = ? AND channel_id = ? and system_id = ?", userId, channelId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-12
 * @version 1.0
 * @desc 我订阅的专栏
 */
func (mapper *ChannelMapper) FindMyChannelsOrderPage(userId uint64, systemId uint64, pageNo int64, pageSize int64) (p *page.Page, err error) {
	// 准备容器对象，开始装载数据库数据
	userBuyChannelList := []model.UserBuyChannel{}
	//创建orm对象
	mysql := orm.NewOrm()
	qs := mysql.QueryTable("xk_channel_user_buy")
	var total int64 = 0
	query := qs.Filter("user_id", userId).Filter("system_id", systemId)
	total, _ = query.Count()
	// 重新换算分页和规则
	page := p.Page(pageNo, pageSize, total)
	// 分页查询
	_, err2 := query.All(&userBuyChannelList)
	// 执行count查询
	if err2 != nil {
		return nil, err
	}
	// 6：把查询的数据放入到分页的records字段，准备返回
	page.Records = userBuyChannelList
	// 7: 如果没有找到直接返回
	if err2 != nil {
		return nil, err2
	}
	return page, nil
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买小册信息
 */
func (mapper *ChannelMapper) SaveUserBuyChannel(userBuyChannel model.UserBuyChannel) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		// 保存用户小册订单
		_, err = txOrm.Insert(&userBuyChannel)
		return err
	})
	return err == nil
}
